<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java IO与NIO深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            padding-bottom: 0.5rem;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2rem;
            border-radius: 16px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: scale(1.05);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
        }
        
        .comparison-table {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .comparison-table th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: 600;
            padding: 1rem;
            text-align: left;
        }
        
        .comparison-table td {
            padding: 1rem;
            border-bottom: 1px solid #e5e7eb;
        }
        
        .comparison-table tr:hover {
            background: #f9fafb;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
            padding: 1.5rem;
            border-radius: 12px;
            margin: 1.5rem 0;
            box-shadow: 0 5px 15px rgba(252, 182, 159, 0.3);
        }
        
        .concept-badge {
            display: inline-block;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.875rem;
            font-weight: 500;
            margin-right: 0.5rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-stream mr-4"></i>Java IO与NIO深度解析
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">探索Java输入输出的演进之路，从传统IO到现代NIO的技术革新</p>
            <div class="flex justify-center space-x-6 text-lg">
                <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-filter backdrop-blur-sm">
                    <i class="fas fa-lock mr-2"></i>同步阻塞 → 同步非阻塞
                </div>
                <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-filter backdrop-blur-sm">
                    <i class="fas fa-tachometer-alt mr-2"></i>性能提升 · 效率优化
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-7xl mx-auto px-6 py-12">
        
        <!-- 概念对比卡片 -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="content-card rounded-2xl p-8">
                <div class="flex items-center mb-4">
                    <div class="w-16 h-16 bg-gradient-to-br from-red-400 to-pink-500 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-ban text-white text-2xl"></i>
                    </div>
                    <div>
                        <h3 class="text-2xl font-bold text-gray-800">BIO (Blocking I/O)</h3>
                        <span class="concept-badge">同步阻塞</span>
                    </div>
                </div>
                <p class="text-gray-600 leading-relaxed">
                    传统的IO模式，采用一个连接一个线程的服务模式。当客户端发起连接请求时，服务器需要启动一个专门的线程进行处理。如果连接空闲，会造成线程资源的浪费。
                </p>
            </div>
            
            <div class="content-card rounded-2xl p-8">
                <div class="flex items-center mb-4">
                    <div class="w-16 h-16 bg-gradient-to-br from-green-400 to-blue-500 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-rocket text-white text-2xl"></i>
                    </div>
                    <div>
                        <h3 class="text-2xl font-bold text-gray-800">NIO (Non-blocking I/O)</h3>
                        <span class="concept-badge">同步非阻塞</span>
                    </div>
                </div>
                <p class="text-gray-600 leading-relaxed">
                    新一代IO模式，采用一个请求一个线程的服务模式。所有连接请求都注册到<strong>多路复用器</strong>上，通过<strong>轮询</strong>机制，只在有<strong>I/O请求时</strong>才<strong>启动</strong>线程处理。
                </p>
            </div>
        </div>

        <!-- NIO核心组件 -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-puzzle-piece mr-3 text-purple-600"></i>NIO核心组件
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="feature-card">
                    <div class="text-3xl mb-4"><i class="fas fa-database"></i></div>
                    <h4 class="text-xl font-bold mb-2">Buffer 缓冲区</h4>
                    <p>包含数据且用于读写的线性表结构，是NIO数据操作的核心载体。</p>
                </div>
                
                <div class="feature-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
                    <div class="text-3xl mb-4"><i class="fas fa-exchange-alt"></i></div>
                    <h4 class="text-xl font-bold mb-2">Channel 通道</h4>
                    <p>双向数据传输通道，包含SocketChannel、FileChannel等多种实现。</p>
                </div>
                
                <div class="feature-card" style="background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);">
                    <div class="text-3xl mb-4"><i class="fas fa-filter"></i></div>
                    <h4 class="text-xl font-bold mb-2">Selector 选择器</h4>
                    <p>多路复用器，将多个异步I/O操作集中到一个或多个线程中处理。</p>
                </div>
            </div>
        </div>

        <!-- 架构对比图 -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-sitemap mr-3 text-purple-600"></i>架构对比
            </h2>
            <div class="mermaid">
                graph LR
                    subgraph "传统 BIO"
                        A[客户端1] -->|阻塞| B[线程1]
                        C[客户端2] -->|阻塞| D[线程2]
                        E[客户端3] -->|阻塞| F[线程3]
                    end
                    
                    subgraph "现代 NIO"
                        G[客户端1] -->|非阻塞| H[Selector<br/>多路复用器]
                        I[客户端2] -->|非阻塞| H
                        J[客户端3] -->|非阻塞| H
                        H -->|轮询| K[线程池]
                    end
                    
                    style A fill:#ff6b6b
                    style C fill:#ff6b6b
                    style E fill:#ff6b6b
                    style G fill:#4ecdc4
                    style I fill:#4ecdc4
                    style J fill:#4ecdc4
                    style H fill:#667eea
            </div>
        </div>

        <!-- Buffer详解 -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-memory mr-3 text-purple-600"></i>Buffer 缓冲区详解
            </h2>
            
            <div class="highlight-box">
                <h4 class="text-xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-key mr-2"></i>核心属性
                </h4>
                <div class="grid md:grid-cols-2 gap-4">
                    <div>
                        <p class="mb-2"><strong>Capacity（容量）：</strong>缓冲区的最大存储容量</p>
                        <p class="mb-2"><strong>Limit（界限）：</strong>缓冲区可操作数据的大小</p>
                    </div>
                    <div>
                        <p class="mb-2"><strong>Position（位置）：</strong>当前读写位置的指针</p>
                        <p class="mb-2"><strong>Mark（标记）：</strong>记录某个特定的位置</p>
                    </div>
                </div>
            </div>
            
            <h4 class="text-xl font-bold mt-8 mb-4 text-gray-800">
                <i class="fas fa-code mr-2"></i>Buffer 操作示例
            </h4>
            <pre class="code-block"><code>// 1.设置缓存区容量大小
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

// 2.写入数